//
//  TwoViewController.m
//  ttxt
//
//  Created by 刘杰辉 on 2017/3/14.
//  Copyright © 2017年 刘杰辉. All rights reserved.
//

#import "TwoViewController.h"
#import "KXYXGameSDK.framework/Headers/KXYXSDKManager.h"
@interface TwoViewController ()

@property (retain, nonatomic) IBOutlet UITextField *textField;
@property (retain, nonatomic) IBOutlet UITextView *textView;
@property (retain, nonatomic) NSString  *logFilePath;
@property (weak, nonatomic) IBOutlet UITextField *role_name;
@property (weak, nonatomic) IBOutlet UITextField *role_server_name;
@property (weak, nonatomic) IBOutlet UITextField *role_id;
@property (weak, nonatomic) IBOutlet UITextField *role_server_id;

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [KXYXSDKManager sharedVOneSDK].logout = ^(){
        [self dismiss:nil];
    };

    [self redirectNSLogToDocumentFolder];
    self.textView.layoutManager.allowsNonContiguousLayout = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(action)];
    
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

int count = 0;
- (void)action
{
    count++;
    if (count == 10) {
        NSString *logString = [NSString stringWithContentsOfFile:self.logFilePath encoding:NSUTF8StringEncoding error:nil];
        
        if (![logString isEqualToString:self.textView.text]) {
            self.textView.text = logString;
            [self.textView scrollRangeToVisible:NSMakeRange(_textView.text.length, 1)];
        }
        
        count = 0;
    }
    
}

- (void)redirectNSLogToDocumentFolder
{
    //如果已经连接Xcode调试则不输出到文件
    //该函数用于检测输出 (STDOUT_FILENO) 是否重定向 是个 Linux 程序方法
    if(isatty(STDOUT_FILENO)) {
        return;
    }
    
    // 判断 当前是否在 模拟器环境 下 在模拟器不保存到文件中
    UIDevice *device = [UIDevice currentDevice];
    if([[device model] hasSuffix:@"Simulator"]){
        return;
    }
    //将NSlog打印信息保存到Document目录下的Log文件夹下
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];
    if (!fileExists) {
        [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //每次启动后都保存一个新的日志文件中
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    _logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];
    
    
    // 将log输入到文件
    freopen([self.logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stdout);
    freopen([self.logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);
    
}




- (IBAction)comfire:(id)sender {
    
    [[KXYXSDKManager sharedVOneSDK] toPay:self
                                      pay:self.textField.text.intValue
                                    goods:@"青龙偃月刀"
                               goods_desc:@"关羽蜀黍"
                            game_order_sn:[NSString stringWithFormat:@"%@",[NSDate dateWithTimeIntervalSinceNow:0]]
                             game_api_url:@"111"
                         game_server_name:self.role_server_name.text
                           game_server_id:self.role_server_id.text
                                role_name:self.role_name.text
                                  role_id:self.role_id.text
                                      ext:@"2012333232" success:^{
        
            NSLog(@"成功");

            
        } failure:^(NSInteger failcode) {
            
            NSLog(@"失败==%ld", (long)failcode);
            
        }];
    
}
- (IBAction)showAlertBindPhone:(id)sender {
    [[KXYXSDKManager sharedVOneSDK] showAlterView];
}

- (IBAction)upLevel:(id)sender {
    
    [[KXYXSDKManager sharedVOneSDK] upgradeWithGameId:27
                                          server_name:self.role_server_name.text
                                            server_id:self.role_server_id.text
                                            role_name:self.role_name.text
                                              role_id:self.role_id.text
                                                level:3
                                     create_role_time:1504664530
                                           completion:^(NSInteger error, NSString *info) {
                                               if (error) {
                                                   //创建角色失败
                                               } else {
                                                   //成功
                                               }
                                               NSLog(@"升级 error = %ld, info = %@", (long)error, info);
                                           }];
}
- (IBAction)recateRole:(id)sender {
    
}
- (IBAction)chatadd:(id)sender {
    
    [[KXYXSDKManager sharedVOneSDK] chataddWithGameId:27 server_name:@"专线一区" server_id:@"1001" role_name:@"大声" role_id:@"1002" vip:@"1" time:@"1402523622" info:@"充值100送200" type:@"1" completion:^(NSInteger error, NSString *info) {
        if (error) {
            //创建角色失败
        } else {
            //成功
        }
        NSLog(@"创建角色 error = %ld, info = %@", (long)error, info);
    }];
    
}

BOOL isShow_ = YES;
- (IBAction)showAssistiveView:(id)sender {
    
    [[KXYXSDKManager sharedVOneSDK] showAssistiveView:!isShow_];
    isShow_ = !isShow_;
}

- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



@end
